#include <fstream>
#include <iostream>
#include <QtCore>
#include <QCoreApplication>
#include "NetworkUtil/Node.h"
#include "NetworkUtil/SocketServer.h"
#include "SemanticUtil/RdfStore.h"

#define PORT_OFFSET 50001

using namespace std;

int Init(int argc, char *argv[]){
	Node localnode_main;
	SocketServer** listenServer;
	if (argc != Node::dimension +2 ){
			std::cout << "The dimension of this cube is " << Node::dimension << ", You have input the wrong number of arg!" <<std::endl;
			return 1;
		}
	std::string mode;
	int portCount = 0;
	mode=argv[Node::dimension+1];
	localnode_main.mode=(mode=="local")?LOCAL:REMOTE;
	for(int dim=0; dim < Node::dimension ; dim++){
		if ( (localnode_main.m[dim]=atoi(argv[dim+1])) > (Node::length-1)){
			std::cout << "The coordinate is exceed the length of the dimension in the " << dim << " dimension" << std::endl ;
			return 1;
		}
	}
	if(localnode_main.mode==LOCAL)
		portCount=(localnode_main.m[0]*9+localnode_main.m[1]*3+localnode_main.m[2])*2*Node::dimension; //如果本地端模拟的话，要加上偏移量
	listenServer = new SocketServer*[2*Node::dimension];  //暂时没考虑用户定义的维数的server个数
	for(int count = 0; count < 2*Node::dimension; count++){

		localnode_main.port[count] = count;
		localnode_main.tcpPort[count] = portCount+PORT_OFFSET + count;           //监听端口
		//设置此SocketServer对象的参数
//		std::cout << "Consturcting the socketServer!" << std::endl;
		listenServer[count] = new SocketServer(count, localnode_main.tcpPort[count], localnode_main );
	}
	return 0;
}

int main(int argc, char *argv[]) //SAFI_Sem_Srv d1 d2 d3 local/remote
{
    QCoreApplication a(argc, argv);


	int error=0;
	error = Init(argc,argv);         //初始化节点
	if (error) {
		return 1;
	}
	else {
		return a.exec();
	}
	
}
